Skip to content

Conversation

rniwa
Copy link
Contributor

@rniwa rniwa commented Aug 22, 2025

Since a temporary object lives until the end of the statement, it's safe to pass such an object as a function argument without explicitly creating a CheckedRef/CheckedPtr in stack.

…ing a temporary object as an argument.

Since a temporary object lives until the end of the statement, it's safe to pass such an object as
a function argument without explicitly creating a CheckedRef/CheckedPtr in stack.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Aug 22, 2025
@rniwa rniwa requested a review from t-rasmud August 22, 2025 21:41
@llvmbot
Copy link
Member

llvmbot commented Aug 22, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: Ryosuke Niwa (rniwa)

Changes

Since a temporary object lives until the end of the statement, it's safe to pass such an object as a function argument without explicitly creating a CheckedRef/CheckedPtr in stack.


Full diff: https://github.com/llvm/llvm-project/pull/155033.diff

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp (+2)
  • (modified) clang/test/Analysis/Checkers/WebKit/unchecked-call-arg.cpp (+5)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
index 478bd85177143..0d2294e1e9bb0 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/ASTUtils.cpp
@@ -208,6 +208,8 @@ bool isASafeCallArg(const Expr *E) {
         return true;
     }
   }
+  if (isa<CXXTemporaryObjectExpr>(E))
+    return true; // A temporary lives until the end of this statement.
   if (isConstOwnerPtrMemberExpr(E))
     return true;
 
diff --git a/clang/test/Analysis/Checkers/WebKit/unchecked-call-arg.cpp b/clang/test/Analysis/Checkers/WebKit/unchecked-call-arg.cpp
index 8685978ebf1ac..b89a3246606ed 100644
--- a/clang/test/Analysis/Checkers/WebKit/unchecked-call-arg.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/unchecked-call-arg.cpp
@@ -32,3 +32,8 @@ void foo() {
   provide()->doWork();
   // expected-warning@-1{{Call argument for 'this' parameter is unchecked and unsafe}}
 }
+
+void doWorkWithObject(const CheckedObject&);
+void bar() {
+  doWorkWithObject(CheckedObject());
+}

Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rniwa rniwa merged commit 49f1751 into llvm:main Sep 8, 2025
12 checks passed
@rniwa rniwa deleted the fix-webkit-unchecked-temporary-call-arg branch September 8, 2025 18:13
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 8, 2025

LLVM Buildbot has detected a new failure on builder ppc64le-flang-rhel-clang running on ppc64le-flang-rhel-test while building clang at step 6 "test-build-unified-tree-check-flang".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/157/builds/39247

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-flang) failure: test (failure)
******************** TEST 'Flang :: Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90' FAILED ********************
Exit Code: 2

Command Output (stdout):
--
# RUN: at line 4
split-file /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/llvm-project/flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp
# executed command: split-file /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/llvm-project/flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp
# RUN: at line 6
/home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/flang -fc1 -emit-hlfir -fopenmp -mmlir --enable-delayed-privatization-staging -fopenmp-version=50 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_module.f90 -o -  | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/flang -fc1 -emit-hlfir -fopenmp -mmlir --enable-delayed-privatization-staging  -fopenmp-version=50 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90 -o -  | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/FileCheck /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90
# executed command: /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/flang -fc1 -emit-hlfir -fopenmp -mmlir --enable-delayed-privatization-staging -fopenmp-version=50 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_module.f90 -o -
# .---command stdout------------
# | module attributes {dlti.dl_spec = #dlti.dl_spec<i64 = dense<64> : vector<2xi64>, i128 = dense<128> : vector<2xi64>, !llvm.ptr = dense<64> : vector<4xi64>, i1 = dense<8> : vector<2xi64>, i8 = dense<8> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, i32 = dense<32> : vector<2xi64>, f16 = dense<16> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, "dlti.endianness" = "little", "dlti.mangling_mode" = "e", "dlti.function_pointer_alignment" = #dlti.function_pointer_alignment<32, function_dependent = true>, "dlti.legal_int_widths" = array<i32: 32, 64>, "dlti.stack_alignment" = 128 : i64>, fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", fir.target_features = #llvm.target_features<["+64bit"]>, llvm.data_layout = "e-m:e-Fn32-i64:64-i128:128-n32:64-S128-v256:256:256-v512:512:512", llvm.ident = "flang version 22.0.0 (https://github.com/llvm/llvm-project.git 49f17515227d8eb8894c13957b642c4200507806)", llvm.target_triple = "powerpc64le-unknown-linux-gnu", omp.is_gpu = false, omp.is_target_device = false, omp.target_triples = [], omp.version = #omp.version<version = 50>} {
# |   fir.global @_QMtest_dataEj : !fir.array<200xi8> {
# |     %0 = fir.zero_bits !fir.array<200xi8>
# |     fir.has_value %0 : !fir.array<200xi8>
# |   }
# |   fir.global @_QMtest_dataEi : !fir.array<10x10xf32> {
# |     %0 = fir.zero_bits !fir.array<10x10xf32>
# |     fir.has_value %0 : !fir.array<10x10xf32>
# |   }
# |   fir.global @_QMtest_dataEz : i32 {
# |     %0 = fir.zero_bits i32
# |     fir.has_value %0 : i32
# |   }
# | }
# `-----------------------------
# .---command stderr------------
# | warning: OpenMP support for version 50 in flang is still incomplete
# `-----------------------------
# executed command: /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/flang -fc1 -emit-hlfir -fopenmp -mmlir --enable-delayed-privatization-staging -fopenmp-version=50 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90 -o -
# .---command stderr------------
# | warning: OpenMP support for version 50 in flang is still incomplete
# | error: Semantic errors in /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90
# | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90:2:9: error: Cannot parse module file for module 'test_data': Source file 'test_data.mod' was not found
# |       use test_data
# |           ^^^^^^^^^
# | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90:7:17: error: No explicit type declared for 'z'
# |           x = y + z + i(1,1) + j(1,1,1) + k(1,1)
# |                   ^
# | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90:7:21: error: No explicit type declared for 'i'
# |           x = y + z + i(1,1) + j(1,1,1) + k(1,1)
# |                       ^
# | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90:7:30: error: No explicit type declared for 'j'
# |           x = y + z + i(1,1) + j(1,1,1) + k(1,1)
# |                                ^
# | /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/tools/flang/test/Lower/OpenMP/DelayedPrivatization/Output/target-private-implicit-scalar-map-2.f90.tmp/imp_scalar_map_target.f90:7:41: error: No explicit type declared for 'k'
# |           x = y + z + i(1,1) + j(1,1,1) + k(1,1)
# |                                           ^
# `-----------------------------
...

@@ -208,6 +208,8 @@ bool isASafeCallArg(const Expr *E) {
return true;
}
}
if (isa<CXXTemporaryObjectExpr>(E))
return true; // A temporary lives until the end of this statement.
Copy link
Collaborator

@haoNoQ haoNoQ Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, it could be lifetime extended. Moreover, since C++17 due to mandatory copy elision it can end up absolutely anywhere. Stack, heap, global, an unknown location into which the function 12 levels above you in the call stack returns, you name it. For example: https://godbolt.org/z/EnvdsWK8G - notice how there are no copy/move-constructors anywhere in that AST. The CXXTemporaryObjectExpr (a sub-class of CXXConstructExpr btw) constructs the object directly at its final destination, no matter how far apart they are in the AST.

The annoying part is that you can't tell that by looking at the construct-expression itself. Normally for a method call the this argument is inside the call expression but for constructors it's somewhere in the parent expression and it could be one of like 30+ different rigid AST variations that you need to take into account, often with an indeterminate amount of irrelevant parent statement layers you need to peel one by one. (Eg. there could be an arbitrary amount of nested ?: operators.) And I'm afraid you can't really tell how long the object lives until you pattern-match the specific AST pattern to figure out the construction target.

Frontend solves it by simply being recursive - so it always has the construction target somewhere up its visitor stack. Maybe that approach would work for you too? You could use the parent map too but that could lead you to some really weird places. For our less recursive analysis machines I've tried to learn how to pattern-match some of these cases in https://clang.llvm.org/doxygen/classclang_1_1ConstructionContext.html but I wasn't wise enough to make it available over pure AST. And it's a very incomplete list anyway.

So the remaining question is, is there a specific way to quickly notice the temporaries that aren't lifetime-extended and ignore the other cases? Basically what you're looking for is the nearest parent MaterializeTemporaryExpr - and then you can ask it whether its getStorageDuration() is SD_FullExpression. The annoying part is that MaterializeTemporaryExpr doesn't have to be there at all - consider C(14, 15) and C(16, 17) in the godbolt example. In this case if you keep using the parent map it may lead you to weird places. So I think your best bet may be to either find an somewhat immediate MaterializeTemporaryExpr or give up and treat the constructor as unsafe. That'd probably handle your test case but it'd fail if doWorkWithObject() accepted it by value. For that you'd need to handle the "passed directly into a CallExpr" case too. But if you don't find either of those almost immediately (either directly above you, or additionally bypassing CXXBindTemporaryExpr which shows up iff the class has a non-trivial destructor IIRC) then it might be better to give up and treat the constructor is unsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, it could be lifetime extended. Moreover, since C++17 due to mandatory copy elision it can end up absolutely anywhere. Stack, heap, global, an unknown location into which the function 12 levels above you in the call stack returns, you name it.

Oh sure, but here what we need is the temporary to at least out-live the end of current statement since what we want to guarantee is that each function argument outlives the function call. It's okay if the temporary's lifetime is extended beyond that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can revise the comment to say it "lives at least until the end of this statement"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooooooooh. Right. You already know it's an argument. Yeah, case closed, the codegen-style recursive approach wins again.

And it looks like you've correctly skipped the MaterializeTemporaryExpr which is probably there in your test because the argument is accepted by reference. There may also be a CXXBindTemporaryExpr if the class has a destructor but it's probably peeled correctly for the same reason.

You folks are really good at this 😅

rniwa added a commit to rniwa/llvm-project that referenced this pull request Sep 9, 2025
…ing a temporary object as an argument. (llvm#155033)

Since a temporary object lives until the end of the statement, it's safe
to pass such an object as a function argument without explicitly
creating a CheckedRef/CheckedPtr in stack.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants